import type { FC } from 'react';
export interface MessageItem {
    Content:
        | string
        | {
              chunk: {
                  choices: any[];
              };
              [key: string]: any;
          };
    Event: string;
    ExtInfo: {
        answer: string;
        msg_features: {
            card_header: {
                // 卡片title 有就展示
                title?: string; // 标题
                sub_title?: string; // 二级标题 暂时没用使用
                card_type?: 'danger' | 'waning'; // 卡片类型 danger:危险 waning:警告 没有就是不展示
            };
            article_code?: string; // 文章code 有就展示
            video_code?: string; // 视频code 有的话优先展示，没有再判断article_code展示
            risk_tips_code?: string; // 诈骗手法code 有就展示
            btns?: Array<// 按钮 有就展示
            {
                text?: string; // 按钮文本
                type?: 'danger' | 'waning' | 'auto_send' | 'share_asst' | 'transfer_manual'; // danger:危险 waning:警告 auto_send：自动发送按钮文本 share_asst：分享助手 transfer_manual人工入口
                value?: string; // 按钮值
            }>;
        };
        msg_id: string;
        pushed_article_codes: any;
        pushed_video_codes: any;
        quick_questions: any;
        recommend_articles: any;
        recommend_questions: any;
        scene_classify: string;
        scene_type: 'all_scene';
        sensitiveType: string;
        status: string;
        status_article_push_history: any;
        status_chat_list: any;
        status_tips_push_history: any;
        trap_classify: string;
        user_status: string;
    };
    From: 'sys' | 'user' | string;
    FromType: 'sys' | 'user' | string;
    MsgType: 'text' | 'agent';
    MsgId: string;
    isHistory: boolean;
    [key: string]: any;
}

export enum InputStatus {
    Empty = '',
    InputStart = 'input-start',
    InputEnd = 'input-end',
    InputTimeout = 'input-timeout'
}
export enum OutputStatus {
    Empty = '',
    InputStart = 'input-start',
    InputEnd = 'input-end',
    InputTimeout = 'input-timeout',
    OutputLoading = 'output-loading',
    OutputStart = 'output-start',
    OutputPending = 'output-pending',
    OutputEnd = 'output-end',
    OutputTimeout = 'output-timeout'
}
export type ChatStatus = 'agent' | 'ai' | 'queue';
export type MessageOptions = {
    userId: string;
    appid: string;
    baseUrl: string;
    token: string;
    openId: string;
    sign: string;
    expireTime: number;
    timeout: number;
};

type UserInfoResult = {
    token: string;
    userId: string;
    appid: string;
};
export type UserInfoParams = {
    openId: string;
    sign: string;
};
export type UserInfoApiType = (params: UserInfoParams) => Promise<[0 | 1, UserInfoResult]>;

type HistoryParams = {
    count?: number;
    userid: string;
    token: string;
    seq?: string;
};
export type HistoryApiType = (params: HistoryParams) => Promise<[0 | 1, MessageInfo[]]>;
export type ChatConfig = {
    openId: string;
    baseUrl: string;
    sign: string;
    expireTime?: number;
    timeout?: number;
};

export type SendApiResult = Record<string, string>;
export type SendParams = {
    sign?: string;
    token?: string;
    Content?: string;
    userid?: string;
    ExtInfo?: Record<string, any>;
    chatStatus?: ChatStatus;
    [key: string]: any;
};

export type SendApiType = (params: SendParams) => Promise<[0 | 1, SendApiResult]>;

export type SendBeforeItemType = (params: SendParams) => Promise<SendParams>;

export type MessageErrorType = {
    message: string;
    type: string;
};
export type OutputRenderStatus = '' | 'start' | 'end';
interface MessageItemExtInfoBtn {
    text?: string;
    type?:
        | 'danger'
        | 'waning'
        | 'auto_send'
        | 'share_asst'
        | 'transfer_manual'
        | 'satisfaction_feedback';
    value?: string;
}
interface MessageItemExtInfo {
    answer?: string;
    msg_features?: {
        card_header?: {
            title?: string;
            sub_title?: string;
            card_type?: 'danger' | 'waning';
        };
        article_code?: string;
        video_code?: string;
        risk_tips_code?: string;
        btns?: Array<MessageItemExtInfoBtn>;
    };
    msg_id?: string;
    pushed_article_codes?: unknown;
    pushed_video_codes?: unknown;
    recommend_articles?: unknown;
    scene_classify?: string;
    scene_type?: 'all_scene';
    sensitiveType?: string;
    status?: string;
    status_article_push_history?: unknown;
    status_chat_list?: unknown;
    status_tips_push_history?: unknown;
    trap_classify?: string;
    user_status?: string;
    recommend_tips?: Array<{ article_code: string; video_code: string; desc: string }>;
    recommend_questions?: string[];
    new_session_quick_questions?: string[];
    quick_questions?: string[];
    session_id?: string;
    infoType?: string;
}

interface MessageItemContent {
    chunk: {
        choices: unknown[];
        [key: string]: unknown;
    };
    userid: string;
    user_msg_id: string;
    eventName: string;
}
type PartialDeepEnhanced<T> = T extends (...args: any[]) => any
    ? T
    : T extends object
      ? T extends Array<infer U>
          ? Array<PartialDeepEnhanced<U>>
          : { [K in keyof T]?: PartialDeepEnhanced<T[K]> }
      : T;
export interface IChatCtx {
    onConnected: () => void;
    chatStatusState: ChatStatus;
    changeChatStatus: (status: string) => void;
    sendMsg: (data: {
        Content: string;
        MsgType?: string;
        Event?: string;
        ExtInfo?: {
            guess_question?: boolean;
            quick_question?: boolean;
        };
    }) => void;
    pushMsg: (
        data: PartialDeepEnhanced<MessageItem> & { chatStatus: string; outputType: string }
    ) => void;
    scrollToBottom: () => void;
}
export type MessageInfo<T = IChatCtx> = Omit<MessageItem, 'Content'> & {
    Content: string | MessageItemContent;
    Event: string;
    ExtInfo: MessageItemExtInfo;
    From: 'sys' | 'user' | string;
    FromType: 'sys' | 'user' | string;
    id: string | number;
    MsgType:
        | 'text'
        | 'agent'
        | 'reset_ai_chat'
        | 'event'
        | 'customGray'
        | 'quitqueue'
        | 'finish'
        | 'queue'
        | 'satisfaction';
    MsgId: string;
    isHistory: boolean;
    ctx?: Partial<T>;
    title?: string;
    onStatusChange?: (status: string) => void;
    triggerType?: string;
    checkKfServiceStatus?: () => Promise<boolean>;
    outputStatus?: string;
    session_id?: string;
};
export type PluginType = (
    input: MessageInfo,
    idx?: number,
    list?: MessageInfo[]
) => void | MessageInfo | MessageInfo[];

export const MessageEvent = {
    Message: 'message',
    Error: 'error',
    Send: 'send',
    End: 'end',
    Start: 'start',
    Overtime: 'overtime',
    Reconnecting: 'reconnecting'
};

export const ErrorType = {
    API: 'api',
    SCRIPT: 'script',
    NETWORK: 'network',
    WEBSOCLKET: 'websocket',
    LOGIC: 'logic'
};
export type InitHistoryStatus = 'error' | 'reset' | 'init' | 'session';

export type ReconnectingInfo = {
    currentRetries: number;
    shouldRetry: boolean;
    delay: number;
};
export type IMsgPredicate = (msg?: MessageInfo) => boolean;
export interface IRenderMsgRule<T = MessageInfo> {
    component: FC<T>;
    match: IMsgPredicate;
}
